<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>文件管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索条件</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="form-search">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">文件名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="filename" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">文件类型</label>
                            <div class="layui-input-inline">
                                <input type="text" name="filetype" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <button id="btn-search" lay-submit lay-filter="btn-search-filter" type="button"
                                 class="layui-btn layui-btn-radius layui-btn-normal">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button  id="btn-reset" type="reset" class="layui-btn layui-btn-radius layui-btn-primary">
                            <i class="layui-icon layui-icon-fonts-clear"></i>
                            重置
                        </button>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
<!--                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>-->
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="file-table" lay-filter="file-table-Filter"></table>

        <script type="text/html" id="table-edit">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="download">下载</a>
<!--            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>-->
        </script>

    </div>
</div>
<script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>

<script>
    function refreshTable() {
        $("#form-reset").click();
        $("#btn-search").click();
    }

    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#file-table',
            url: '/filedDownload/list',
            toolbar: '#toolbarDemo',
            cols: [
                [
                    {type: "checkbox", width: 50},
                    {field: 'id', width: 80, title: 'id', sort: true,align:"center"},
                    {field: 'filename', width: 250, title: '文件名称',align:"center"},
                    {field: 'filesize', width: 100, title: '文件大小',align:"center"},
                    {field: 'filetype', width: 100, title: '文件类型',align:"center"},
                    {field: 'filesrc', width: 550, title: '文件目录',align:"center"},
                    {title: '操作', minWidth: 50, toolbar: '#table-edit', align: "center"}
                ]
            ],
            limits: [1, 5, 10, 30, 50, 100],
            limit: 10,
            page: true,
        });
        //数据显示OK
        form.on('submit(btn-search-filter)', function (data) {
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            table.reload('file-table', {
                where: { //设定异步数据接口的额外参数，任意设
                    filename: data.field.filename,
                    filetype:data.field.filetype
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

    //     /**
    //      * toolbar监听事件
    //      */
        //添加和删除功能
        table.on('toolbar(file-table-Filter)', function (obj) {
            if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('file-table'), data = checkStatus.data;
                if(data.length==0)
                {
                    layer.alert("未选中任何数据",{icon:2,anim:6,title:"警告"});
                    return false;
                }
                var idArr=new Array();
                $(data).each(function () {
                    idArr.push(this.id);
                });
                layer.confirm('请确认删除选中的记录', {icon: 3, title:'确认'}, function (index) {
                    //异步删除,成功提示,刷新表格
                    url="[[@{/}]]"+"filedDownload/delete/"+idArr.join(",");
                    var params={
                        _method:"DELETE"
                    }
                    $.post(url,params,function (res) {
                        if(res.code==0)
                        {
                            layer.msg(res.message,{icon:1,title:"操作结果"});
                            refreshTable();
                        }
                    })
                    layer.close(index);
                });
            }
        });

        //下载方法
        table.on('tool(file-table-Filter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'download') {
                layer.confirm('请确认要下载该文件吗:'+data.filename+" ?", {icon: 3, title:'确认'},
                    function (index) {
                    window.location.href="filedDownload/down?id="+data.id;
                        layer.close(index);
                })
                ;
                return false;
            }
        });
    });
</script>

</body>
</html>